//
//  HomeRootTopView.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2022/8/29.
//

#import "HomeRootTopView.h"

#define WHITEHEIGHT 200.00//背景色渐变为白色时的高度
#define SIZE_NORMAL [UIFont systemFontOfSize:16]
#define SIZE_LARGE [UIFont boldSystemFontOfSize:20]
#define BTNTAG 100

@interface HomeRootTopView ()

@property (nonatomic, assign) NSInteger currentIndex;//当前显示的页面 0推荐 1排行榜 默认0

@end

@implementation HomeRootTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homeroot_scrollViewDidScroll:) name:@"HomeRoot_scrollViewDidScroll" object:nil];
        
        UIButton *btn0 = [[UIButton alloc] init];
        [btn0 setTitle:@"推荐" forState:UIControlStateNormal];
        btn0.titleLabel.font = SIZE_LARGE;
        [btn0 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn0.tag = BTNTAG + 0;
        [btn0 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn0];
        [btn0 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(14);
            make.bottom.equalTo(self).offset(-10);
        }];
        
        UIButton *btn1 = [[UIButton alloc] init];
        [btn1 setTitle:@"排行榜" forState:UIControlStateNormal];
        btn1.titleLabel.font = SIZE_NORMAL;
        [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn1.tag = BTNTAG + 1;
        [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn1];
        [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(btn0.mas_right).offset(14);
            make.centerY.equalTo(btn0);
        }];
        
        UIView *topView = [[UIView alloc] init];
        [self addSubview:topView];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.mas_equalTo(UIDevice.vg_navigationBarHeight);
            make.top.mas_equalTo(UIDevice.vg_statusBarHeight);
        }];
        
        UIButton *rightBtn0 = [[UIButton alloc] init];
        rightBtn0.tag = BTNTAG + 2;
        [rightBtn0 setImage:[UIImage imageNamed:@"ic_home_page_notification_white"] forState:UIControlStateNormal];
        [rightBtn0 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:rightBtn0];
        [rightBtn0 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.height.equalTo(topView);
            make.width.equalTo(rightBtn0.mas_height);
            make.right.equalTo(topView).offset(-5);
        }];
        UIButton *rightBtn1 = [[UIButton alloc] init];
        rightBtn1.tag = BTNTAG + 3;
        [rightBtn1 setImage:[UIImage imageNamed:@"ic_detail_toolbar_download_white"] forState:UIControlStateNormal];
        [rightBtn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:rightBtn1];
        [rightBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.height.equalTo(topView);
            make.width.equalTo(rightBtn1.mas_height);
            make.right.equalTo(rightBtn0.mas_left);
        }];
        
        UIView *searchView = [[UIView alloc] init];
        searchView.tag = BTNTAG + 4;
        searchView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
        searchView.layer.cornerRadius = (UIDevice.vg_navigationBarHeight - 10)/2;
        searchView.layer.masksToBounds = YES;
        [searchView addTapBlock:^(id  _Nonnull obj) {
            NSLog(@"去搜索");
        }];
        [topView addSubview:searchView];
        [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(topView).offset(14);
            make.centerY.equalTo(topView);
            make.top.equalTo(topView).offset(5);
            make.right.equalTo(rightBtn1.mas_left).offset(-10);
        }];
        UIImageView *searchImage = [[UIImageView alloc] init];
        searchImage.tag = BTNTAG + 5;
        searchImage.image = [UIImage imageNamed:@"ic_home_page_search_white"];
        [searchView addSubview:searchImage];
        [searchImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(15);
            make.left.equalTo(searchView).offset(10);
            make.centerY.equalTo(searchView);
        }];
        UILabel *searchLabel = [[UILabel alloc] init];
        searchLabel.tag = BTNTAG + 6;
        searchLabel.textColor = [UIColor whiteColor];
        searchLabel.font = [UIFont systemFontOfSize:12];
        searchLabel.text = @"输入你想玩的游戏";
        [searchView addSubview:searchLabel];
        [searchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(searchView);
            make.left.equalTo(searchImage.mas_right).offset(10);
        }];
    }
    return self;
}

- (void)btnClick:(UIButton *)button
{
    if (button.tag == BTNTAG) {
        //推荐
        if (self.currentIndex == 0) {
            return;
        }
        UIButton *btn0 = [self viewWithTag:button.tag];
        btn0.titleLabel.font = SIZE_LARGE;
        UIButton *btn1 = [self viewWithTag:BTNTAG + 1];
        btn1.titleLabel.font = SIZE_NORMAL;
        [self homeroottop_scrollViewDidScroll:0];
        self.currentIndex = 0;
        if (self.homeroot_btnClickBlock) {
            self.homeroot_btnClickBlock(button.tag);
        }
    } else if (button.tag == BTNTAG + 1) {
        //排行榜
        if (self.currentIndex == 1) {
            return;
        }
        UIButton *btn1 = [self viewWithTag:button.tag];
        btn1.titleLabel.font = SIZE_LARGE;
        UIButton *btn0 = [self viewWithTag:BTNTAG + 0];
        btn0.titleLabel.font = SIZE_NORMAL;
        [self homeroottop_scrollViewDidScroll:WHITEHEIGHT];
        self.currentIndex = 1;
        if (self.homeroot_btnClickBlock) {
            self.homeroot_btnClickBlock(button.tag);
        }
    } else if (button.tag == BTNTAG + 2) {
        //通知
        NSLog(@"通知");
        
    } else if (button.tag == BTNTAG + 3) {
        //下载
        NSLog(@"下载");
    }
    
    
}

- (void)homeroot_scrollViewDidScroll:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSString *scroll_y = userInfo[@"scroll_y"];
    [self homeroottop_scrollViewDidScroll:scroll_y.floatValue];
    
}

- (void)homeroottop_scrollViewDidScroll:(CGFloat)scroll_y
{
    if (scroll_y < 0) {
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0];
    } else if (scroll_y > WHITEHEIGHT) {
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0];
    } else {
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:scroll_y/WHITEHEIGHT];
        
        UIButton *btn0 = [self viewWithTag:BTNTAG + 0];
        UIButton *btn1 = [self viewWithTag:BTNTAG + 1];
        UIButton *btn2 = [self viewWithTag:BTNTAG + 2];
        UIButton *btn3 = [self viewWithTag:BTNTAG + 3];
        UIView *searchView = [self viewWithTag:BTNTAG + 4];
        UIImageView *searchImage = [self viewWithTag:BTNTAG + 5];
        UILabel *searchLabel = [self viewWithTag:BTNTAG + 6];
        
        if (scroll_y/WHITEHEIGHT < 0.5) {
            [btn0 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn2 setImage:[UIImage imageNamed:@"ic_home_page_notification_white"] forState:UIControlStateNormal];
            [btn3 setImage:[UIImage imageNamed:@"ic_detail_toolbar_download_white"] forState:UIControlStateNormal];
            searchView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
            searchImage.image = [UIImage imageNamed:@"ic_home_page_search_white"];
            searchLabel.textColor = [UIColor whiteColor];
        } else {
            [btn0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn2 setImage:[UIImage imageNamed:@"ic_home_page_notification"] forState:UIControlStateNormal];
            [btn3 setImage:[UIImage imageNamed:@"ic_home_page_download"] forState:UIControlStateNormal];
            searchView.backgroundColor = kColor(@"F5F5F6");
            searchImage.image = [UIImage imageNamed:@"ic_home_page_search_gray"];
            searchLabel.textColor = kColor888888;
        }
    }
}


+(CGFloat)height
{
    return 150;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"HomeRoot_scrollViewDidScroll" object:nil];
}

@end
